#include <pfcGlobal.h>
#include <wfcClient.h>
#include <uifcDefaultListeners.h>
#include <logger.h>
#include <wxyutils.h>
#include "main.h"
#include <fstream>
#include <io.h>
#include <windows.h>
#ifdef GetMessage
#define GetMessage GetMessage
#endif
wfcStatus OpenDrw();

//程序入口
extern "C" int user_initialize(
    int argc,
    char *argv[],
    char *version[],
    char *build,
    wchar_t errbuf[80])
{
    string dllpath;
    GetDllPath(dllpath);
    for (int i = 0; i < 3; i++)
    {
        LogFile[i] = dllpath + LogFile[i];
    }
    initLogger(LogFile[0], LogFile[1], LogFile[2]);

    pfcSession_ptr session = pfcGetProESession();
    try
    {
        pfcUICommand_ptr OpenDrw_com = CreateUICommand(session, "OpenDrw_com", new MycallbackClass(OpenDrw),
                                                       MSGFILE, "OpenDrw", "OpenDrw", "OpenDrw",
                                                       new MyActionAccess(pfcACCESS_AVAILABLE));
        //设置快捷键
        session->SetConfigOption("mapkey", "odr ~ Command `OpenDrw_com`;");
    }
    OTK_EXCEPTION_PRINT_LOG
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}
//程序出口
extern "C" void user_terminate()
{
    LOG(LOG_FATAL); //触发logger析构函数
    fstream file;
    for (int i = 0; i < 3; i++)
    {
        file.open(LogFile[i], ios::in);
        if (file.get() == EOF)
        {
            file.close();
            file.clear();
            _FileExt emptyfile(LogFile[i].c_str());
            DeleteFileA(emptyfile.fullname.c_str());
        }
        else
        {
            file.close();
            file.clear();
        }
    }
}
